package com.chouren.goandroid.http.response;

import java.io.File;
import java.util.Arrays;

import android.graphics.Bitmap;

import com.chouren.goandroid.http.client.HttpClient;
import com.chouren.goandroid.http.client.HttpClientConfig;
import com.chouren.goandroid.http.comm.KeyValuePair;
import com.chouren.goandroid.http.exception.HttpException;
import com.chouren.goandroid.http.parser.BinaryParser;
import com.chouren.goandroid.http.parser.BitmapParser;
import com.chouren.goandroid.http.parser.DataParser;
import com.chouren.goandroid.http.parser.FileParser;
import com.chouren.goandroid.http.parser.StringParser;
import com.chouren.goandroid.http.request.Request;

public class InternalResponse implements Response {
	protected HttpStatus httpStatus;
	protected String charSet = HttpClientConfig.DEFAULT_CHARSET;
	protected int tryTimes;
	protected int redirectTimes;
	protected int readedLength;
	protected long contentLength;
	protected long connectTime;
	protected KeyValuePair[] headers;
	protected Request request;
	protected DataParser<?> dataParser;
	protected HttpClient.ExecuteListener executeListener;
	protected HttpException exception;
	
	@Override
	public String toString() {
		return "InternalResponse [httpStatus=" + httpStatus + ", charSet="
				+ charSet + ", tryTimes=" + tryTimes + ", redirectTimes="
				+ redirectTimes + ", readedLength=" + readedLength
				+ ", contentLength=" + contentLength + ", connectTime="
				+ connectTime + ", headers=" + Arrays.toString(headers)
				+ ", request=" + request + ", dataParser=" + dataParser
				+ ", executeListener=" + executeListener + ", exception="
				+ exception + "]";
	}

	@Override
	public String getString() {
		if (dataParser instanceof StringParser) {
			return ((StringParser) dataParser).getData();
		} else {
			throw new RuntimeException("get string , your Request must use StringParser");
		}
	}

	@Override
	public byte[] getBytes() {
		if (dataParser instanceof BinaryParser) {
			return ((BinaryParser) dataParser).getData();
		} else {
			throw new RuntimeException("get bytes , your Request must use BinaryParser");
		}
	}

	@Override
	public <T> T getObject(Class<T> clazz) {
		throw new RuntimeException("the method is not supported yet");
	}

	@Override
	public <T> T getObjectWithMockData(Class<T> claxx, String json) {
		throw new RuntimeException("the method is not supported yet");
	}

	@Override
	public File getFile() {
		if (dataParser instanceof FileParser) {
			return ((FileParser) dataParser).getData();
		} else {
			throw new RuntimeException("get bytes , your Request must use BitmapParser");
		}
	}

	@Override
	public Bitmap getBitmap() {
		if (dataParser instanceof BitmapParser) {
			return ((BitmapParser) dataParser).getData();
		} else {
			throw new RuntimeException("get bytes , your Request must use BitmapParser");
		}
	}

	@Override
	public KeyValuePair[] getHeaders() {
		return headers;
	}

	@Override
	public long getContentLength() {
		return contentLength;
	}

	@Override
	public String getCharSet() {
		return charSet;
	}

	@Override
	public Request getRequest() {
		return request;
	}

	@Override
	public HttpException getException() {
		return exception;
	}

	@Override
	public HttpStatus getHttpStatus() {
		return null;
	}

	@Override
	public boolean isSuccess() {
		return false;
	}

	@Override
	public int getTryTimes() {
		return tryTimes;
	}

	@Override
	public int getRedirectTimes() {
		return redirectTimes;
	}

	@Override
	public long getConnectTime() {
		return connectTime;
	}

	@Override
	public int getReadedLength() {
		return (int) contentLength;
	}

	@Override
	public DataParser<?> getDataParser() {
		return this.dataParser;
	}
	
	public void setExecuteListener(HttpClient.ExecuteListener listener) {
		this.executeListener = listener;
	}

	public void setConnectTime(long connectTime) {
		this.connectTime = connectTime;
	}

	public void setRequest(Request request) {
		this.request = request;
	}

	public void setDataParser(DataParser<?> dataParser) {
		this.dataParser = dataParser;
	}

	public HttpClient.ExecuteListener getExecuteListener() {
		return executeListener;
	}

	public void setHttpStatus(HttpStatus httpStatus) {
		this.httpStatus = httpStatus;
	}

	public void setCharSet(String charSet) {
		this.charSet = charSet;
	}

	public void setTryTimes(int tryTimes) {
		this.tryTimes = tryTimes;
	}

	public void setRedirectTimes(int redirectTimes) {
		this.redirectTimes = redirectTimes;
	}

	public void setReadedLength(int readedLength) {
		this.readedLength = readedLength;
	}

	public void setContentLength(long contentLength) {
		this.contentLength = contentLength;
	}

	public void setHeaders(KeyValuePair[] headers) {
		this.headers = headers;
	}

	public void setException(HttpException exception) {
		this.exception = exception;
	}
	
}
